define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-product',
				'urlDelete': 'product/remove',
				'view_model': {
					'view': {
						'addButton': pubjs.checkRight('product.add')
					}
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();

			self.append($([
				'<div class="M-formHead">',
					// '<span class="M-formHeadTitle">'+LANG('产品报表')+'</span>',
					'<span ms-visible="view.addButton" class="ml10 addButton" pub-name="addButton" pub-mod="@base/common/drop.add"></span>',
					'<span class="ml10" pub-name="batchAdd" pub-mod="@base/common/drop.add" ></span>',
					'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="product/grid/product.base"/>'
			].join('')));
			self.buildFromTemplate({
				'highGrid': {
					'hasMenu': true,
					'hasAutoRefresh': true,
					'hasSearch': true,
					'gridName': 'product'
				},
				'date': {
					'pos': 'bR'
				},
				'addButton': {
					'def': LANG('新建产品'),
					'href': '#product/edit'
				},
				'batchAdd': {
					'def': LANG('批量导入'),
					'icon': 'icon-setting2',
					'useCallback': true
				}
			});

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'attr': {'data-tab': self._.name},
				'items': [
/*					{'name': 'label', 'uri':'modules/sideBarBase.label', 'config': {
						url: 'product/listLabel'
					}},*/
					{'name': 'metrics', 'uri':'modules/sideBarBase.metrics', 'config': {
						'metrics': ["{front}","{back}","{conversion}","{cost}"]
					}}
				]
			});
		},
		// 点击新建产品
		onInputClick: function( ev ) {
			var self = this;

			if(ev.name == 'batchAdd'){
				// 批量导入
				var mod = self.get('batchAddPop');
				if(!mod){
					self.createAsync('batchAddPop', 'product/popwin/product.batchAddPop', {}, function(module){
						if(module){
							module.show();
						}
					});
				}else{
					mod.reset().show();
				}
			}

		},
		// 操作列点击事件
		onOperateMenuShow: function( ev ) {
			var param = ev.param;
			var result = [];
			if (pubjs.checkRight('product.update')) {
				result.push({'id': 'edit', 'name': LANG('编辑')});
			}

			if (pubjs.checkRight('product.remove')) {
				if( param.IsImport ) {
					result.push({'id': 'delete', 'name': LANG('删除')});
				}
			}
			ev.returnValue = result;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function( ev ) {
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var c = this.getConfig();
			var self = this;
			switch(key){
				case 'edit':
					window.open( '#product/edit/' + id );
				break;
				case 'delete':
					pubjs.confirm(LANG('确认删除产品 %1 吗', id), function(state) {
						if(state)
						{
							pubjs.loading.show();
							pubjs.mc.send(c.urlDelete, {'_id': +id}, function(err) {
								pubjs.loading.hide();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
				break;
			}
		},
		reload: function( param ) {
			this.$.highGrid.reload();
		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'label':
						param.Labels = id;
						break;
					case 'metrics':
						param.metrics_filter = id;
						break;
				}
			});

			self.$.highGrid.reload(param);
		}
	});
	exports.main = List;
});